home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf / NumbersUp_Game / numbersup.c < prev    next >
C/C++ Source or Header  |  1992-03-30  |  12KB  |  628 lines

  1.  
  2. #include <intuition/intuition.h>
  3. #include <stdio.h>
  4. #include "includesound.h"
  5. #include "imagec"
  6. #include "infoc"
  7. #include "titlec"
  8. #include "questionc"
  9. #include "gameoverc"
  10. #include "sound"
  11.  
  12. #define HEIGHT 203
  13. #define WIDTH 320
  14. #define DEPTH 5
  15.  
  16. struct Window *win;
  17. struct Screen *scr;
  18. struct IntuitionBase *IntuitionBase;
  19. struct GfxBase *GfxBase;
  20. struct ViewPort *vp;
  21. struct RastPort *rp;
  22. struct IntuiMessage *message;
  23.  
  24. USHORT class;
  25. USHORT code;
  26.  
  27. struct NewScreen ns =
  28. { 0,0,WIDTH,HEIGHT,DEPTH,0,1,NULL,  /* View modes */
  29.   CUSTOMSCREEN,  /* Screen type */
  30.   NULL,          /* Font */
  31.   NULL,NULL,NULL          /* CustomBitmap */  };
  32.  
  33. struct NewWindow nw =
  34. { 0,0,WIDTH,HEIGHT,0,1,MOUSEBUTTONS+VANILLAKEY,  /* IDCMP flags */
  35.   ACTIVATE|BORDERLESS|RMBTRAP, /* Flags */
  36.   NULL,NULL,NULL,  /* Gadget,Checkmark,Name */
  37.   NULL,NULL,  /* Screen,BitMap */
  38.   WIDTH,HEIGHT,100,100,  /* Max Width,Height, Min Width,Height */
  39.   CUSTOMSCREEN  };
  40.  
  41. char names[12][80];
  42. int scores[12];
  43.  
  44. int save_to_disk=1;
  45.  
  46. char dis[15];
  47. int loc[50][50];
  48. int number,dummy,xi,xpos,score=0;
  49. int rawkey=1;
  50.  
  51. UWORD pointer_data[] = { 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000 };
  52.  
  53. main()
  54. {
  55. int input;
  56. FILE *testing123;
  57.  
  58. Open_Stuff();
  59. SetPointer(win,&pointer_data,1,1,0,0);
  60.  
  61. SetRGB4(vp,1,10,3,8);
  62. SetRGB4(vp,2,15,0,0);
  63. SetRGB4(vp,3,0,15,0);
  64. SetRGB4(vp,4,0,0,15);
  65. SetRGB4(vp,5,7,7,7);
  66. SetRGB4(vp,6,15,0,15);
  67. SetRGB4(vp,7,0,15,15);
  68. SetRGB4(vp,8,15,15,0);
  69. SetRGB4(vp,9,0,15,7);
  70. SetRGB4(vp,10,15,9,9);
  71. SetRGB4(vp,11,15,15,15);
  72. SetRGB4(vp,20,15,15,0);
  73. SetRGB4(vp,22,15,0,0);
  74. SetRGB4(vp,25,9,9,9);
  75. SetRGB4(vp,29,15,15,0);
  76.  
  77. Move(rp,120,100);
  78. Text(rp,"Loading...",10);
  79.  
  80. Save_to_disk();
  81.  
  82. SetAPen(rp,1);
  83.  
  84. DrawImage(rp,&title,0,0);
  85.  
  86. InitRandom();
  87. InitLoc();
  88. InitScreen();
  89. ClearSelected();
  90. InitHighScores();
  91. while(1)
  92.     {
  93.     DrawImage(rp,&image,52,67);
  94.     while((input=idcmpch())==0);
  95.       if(input==1) NumbersUp();
  96.         else if(input==81) die("");
  97.            else if(input==2) ViewHighScores();
  98.               else if(input==73) ViewInstructions();
  99.    }
  100. }
  101.  
  102.  
  103. Save_to_disk()
  104. {
  105. int i;
  106. DrawImage(rp,&saved,71,40);
  107. while(1)
  108.    {
  109.    while((i=idcmpch())==0);
  110.    if(i==1) { save_to_disk=1; ClearAll(); return(); }
  111.    else if(i==2) { save_to_disk=0; ClearAll(); return(); }
  112.    }
  113. }
  114.  
  115. ViewInstructions()
  116. {
  117. DrawImage(rp,&info,50,65);  /* This simply draws the info to the window */
  118. while(idcmpch()!=1);
  119. return();
  120. }
  121.  
  122. ViewHighScores()
  123. {
  124. int m,color=1;
  125. char numy[100];
  126.  
  127. ClearSelected();
  128. SetAPen(rp,20);
  129. for(m=0;m<10;m++)
  130.    {
  131.    Move(rp,100,90+m*10);
  132.    Text(rp,names[m],strlen(names[m]));
  133.    Move(rp,193,90+m*10);
  134.    sprintf(numy,"%d",scores[m]);
  135.    Text(rp,numy,strlen(numy));
  136.    SetAPen(rp,color++);
  137.    }
  138. SetAPen(rp,22);
  139. Move(rp,113,75); Text(rp,"HIGH SCORES",11);
  140. Move(rp,75,200); Text(rp,"Press LMB to continue",21);
  141. while(idcmpch()!=1);
  142. ClearSelected();
  143. }
  144.  
  145. ClearSelected()
  146. {
  147. struct Image i ={
  148. 0,0,288,137,1,NULL,0,0,NULL };
  149. DrawImage(rp,&i,16,65);
  150. }
  151.  
  152. ClearAll()
  153. {
  154. struct Image i={
  155. 0,0,320,203,1,NULL,0,0,NULL };
  156. DrawImage(rp,&i,0,0);
  157. }
  158.  
  159. NumbersUp()
  160. {
  161.  
  162. int dummy2,gameover=1,nopress=1;
  163. int input;
  164. score=0;
  165. SetAPen(rp,1);
  166. DisplayScore();
  167. InitRandom();
  168. InitLoc();
  169. InitScreen();
  170. ClearSelected();
  171. while(gameover)
  172.   {
  173.   if(!nopress)
  174.      {
  175.      score-=100;
  176.      if(score<0) score=0;
  177.      DisplayScore();
  178.      }
  179.  
  180.   number=rand()%2;
  181.   if(number==1)
  182.      {
  183.      number=rand()%9+1;
  184.      sprintf(dis," %c",number+48);
  185.      xi=4;
  186.      xpos=0;
  187.      }
  188.      else
  189.      {
  190.      number=rand()%9+1;
  191.      sprintf(dis,"%c ",number+48);
  192.      xpos=304;
  193.      xi=-4;
  194.      }
  195.  
  196.   SetAPen(rp,number+1);
  197.   dummy=1;
  198.   while(dummy)
  199.      {
  200.      xpos+=xi;
  201.      Move(rp,xpos,200);
  202.      Text(rp,dis,2);
  203.      dummy2=xpos;
  204.      if(xi==4)
  205.         {
  206.         if(xpos%8!=0) dummy2=xpos+12;
  207.           else dummy2=xpos+8;
  208.         }
  209.         else
  210.         if(xpos%8!=0) dummy2=xpos-4;
  211.  
  212.      nopress=1;
  213.      input=idcmpch();
  214.  
  215.      if(input==1||input==-2)
  216.       {
  217.       if(loc[(dummy2-16)/8+1][14]==0)
  218.         {
  219.         dummy=0;
  220.         if(up()==9) gameover=0;
  221.         }
  222.       WaitTOF();
  223.       }
  224.         else
  225.         {
  226.         WaitTOF();
  227.         if(xi==4&&xpos==288)
  228.            {
  229.            dummy=0;
  230.            Move(rp,xpos,200);
  231.            Text(rp,"  ",2);
  232.            nopress=0;
  233.            }
  234.            else
  235.            if(xi==-4&&xpos==16)
  236.            {
  237.            dummy=0;
  238.            Move(rp,xpos,200);
  239.            Text(rp,"  ",2);
  240.            nopress=0;
  241.            }
  242.         }
  243.      }
  244.   }
  245. if((CheckHighScores())==0) displayend();
  246. }
  247.  
  248. displayend()
  249. {
  250. SetAPen(rp,2);
  251. DrawImage(rp,&gameover,93,158);
  252. Move(rp,75,180); Text(rp,"Press LMB to continue",21);
  253. while(idcmpch()!=1);
  254. return();
  255. }
  256.  
  257. CheckHighScores()
  258. {
  259. int m,place,dy,input,w=0;
  260. char temp[100],blanks[100];
  261.  
  262. if(score<=scores[9]) return(0);  /* The user hasn't got a high score! */
  263.  
  264. memset((void *)temp,0,sizeof(temp));
  265.  
  266. for(m=0;m<10;m++)
  267.   if(score>scores[m])
  268.      {
  269.      place=m;
  270.      m=10;
  271.      }
  272. dy=1;
  273. DrawImage(rp,&gameover,93,158);
  274. SetAPen(rp,20);
  275. Move(rp,75,180);
  276. Text(rp,"You got a high score",20);
  277. Move(rp,67,190);
  278. Text(rp,"Please enter your name",22);
  279. Move(rp,90,200);
  280. Text(rp,"NAME:",5);
  281. while(dy)
  282.    {
  283.    while((input=idcmpch())==0);
  284.    if(input==13) dy=0;
  285.      else if(input<=97&&input>=65&&w<=10)
  286.      {
  287.      temp[w++]=input;
  288.      Move(rp,140,200);
  289.      Text(rp,temp,strlen(temp));
  290.      }
  291.      else if(input==8&&w>0)
  292.      {
  293.      temp[--w]='\0';
  294.      Move(rp,140,200);
  295.      sprintf(blanks,"%s                         ",temp);
  296.      Text(rp,blanks,24);
  297.      }
  298.    }
  299.  
  300. if(place==9) { scores[place]=score; strcpy(names[place],temp); }
  301. else
  302. {
  303. for(m=8;m>=place;m--)
  304.    {
  305.    scores[m+1]=scores[m];
  306.    strcpy(names[m+1],names[m]);
  307.    }
  308. scores[place]=score;
  309. strcpy(names[place],temp);
  310. }
  311.  
  312. ClearSelected();
  313. if(save_to_disk) CreateNew();
  314. ViewHighScores();   /* Show the user the high scores when he/she gets one */
  315. return(1);
  316. }
  317.  
  318. InitHighScores()
  319. {
  320. FILE *high;
  321. if((high=fopen("highs","r"))==NULL)
  322.   {
  323.   CreateBrandNew();
  324.   }
  325.   else
  326.   {
  327.   fclose(high);
  328.   LoadHighScores();
  329.   }
  330. }
  331.  
  332. LoadHighScores()
  333. {
  334. char temp[10],c;
  335. FILE *high;
  336. int m,o;
  337.  
  338. high=fopen("highs","r");
  339. rewind(high);
  340.  
  341. for(m=0;m<10;m++)
  342.   {
  343.   o=0;
  344.     while((c=getc(high))!=12)
  345.     names[m][o++]=c;
  346.  
  347.     memset((void *)temp,0,sizeof(temp));
  348.   o=0;
  349.     while((c=getc(high))!=12)
  350.     temp[o++]=c+34;
  351.   scores[m]=atoi(temp);
  352.   }
  353. fclose(high);
  354. return();
  355. }
  356.  
  357. CreateBrandNew()   /* If there is no high score file I create one */
  358. {                  /* Delete the file "highs" and see what happens */
  359. FILE *high;
  360. char temp[1000],dum[100];
  361. int m;
  362. memset((void *)temp,0,sizeof(temp));
  363. for(m=0;m<10;m++)
  364.  {
  365.  sprintf(dum,"Jason%c%c%c%c%c%c%c",12,14,14,15,14,14,12);
  366.  strcpy(names[m],"Jas");
  367.  scores[m]=100;
  368.  strcat(temp,dum);
  369.  }
  370.  
  371. if(save_to_disk)
  372. if((high=fopen("highs","w"))==NULL)
  373.   die("   I NEED YOUR DISK TO BE WRITE ENABLED SO I CAN CREATE A HIGHSCORES TABLE!!!");
  374.   else
  375.   {
  376.   fwrite(temp,1,strlen(temp),high);
  377.   fclose(high);
  378.   }
  379. }
  380.  
  381. CreateNew()
  382. {
  383. int n,n1,n2,m,length,when=0;
  384. char tem[1000],tem2[10];
  385. FILE *file;
  386. memset((void *)tem,0,sizeof(tem));
  387. memset((void *)tem2,0,sizeof(tem2));
  388.  
  389. if((file=fopen("highs","w"))==NULL)
  390.    when=TRUE;
  391.  
  392. for(m=0;m<10;m++)
  393.  {
  394.  strcat(tem,names[m]);
  395.  tem[strlen(tem)]=12;
  396.  n=scores[m];
  397.  length=0;
  398.   for(n1=4;n1>=0;n1--)
  399.      {
  400.      n2=n%10;
  401.      n=(n-n2)/10;
  402.      tem2[length++]=n2+14;
  403.      }
  404.  length=strlen(tem);
  405.   for(n1=4;n1>=0;n1--)
  406.   tem[length++]=tem2[n1];
  407.   tem[strlen(tem)]=12;
  408.   }
  409.  
  410. if(when==TRUE) return();
  411.  
  412.   fwrite(tem,1,strlen(tem),file);
  413.   fclose(file);
  414. return();
  415. }
  416.  
  417.  
  418. InitRandom()
  419. {
  420. ULONG sec,micros;
  421. CurrentTime(&sec,µs);
  422. srand(sec*99+micros);
  423. return();
  424. }
  425.  
  426. up()
  427. {
  428. register int y,where,ttt,stop;
  429. int num1,num2,num3,num;
  430. char buf2[80];
  431.  
  432. if(xpos%8!=0)
  433.   {
  434.   xpos+=xi;
  435.   Move(rp,xpos,200); Text(rp,dis,2);
  436.   }
  437.  
  438. if(xi==4) xpos+=8;
  439. sprintf(dis,"%c",number+48);
  440.  
  441. where=(xpos-16)/8;
  442. where++;
  443.  
  444. for(ttt=0;ttt<50;ttt++)
  445.  if(loc[where][ttt+1]==0)
  446.     {
  447.     loc[where][ttt+1]=number;
  448.     stop=ttt;
  449.     ttt=50;
  450.     }
  451.  
  452. num=loc[where][stop+1];
  453.  
  454. num1=loc[where][stop];
  455. num2=loc[where+1][stop+1];
  456. num3=loc[where-1][stop+1];
  457.  
  458. ttt=stop*9+73;
  459.  
  460. Move(rp,xpos,200); Text(rp," ",1);
  461.  
  462. for(y=200;y>=ttt;y--)
  463.  {
  464.  Move(rp,xpos,y);
  465.  Text(rp,dis,1);
  466.  }
  467.  
  468.  
  469. if(num1==num||num2==num||num3==num)
  470.   {
  471.   StopSound(LEFT0);
  472.   PlaySound(&boing,MAXVOLUME,LEFT0,NORMALRATE,1,0,0);
  473.   return(9);
  474.   }
  475. else
  476.   {
  477.   StopSound(LEFT0);
  478.   PlaySound(&boing,MAXVOLUME,LEFT0,NORMALRATE,1,0,0);
  479.   score+=((num1+num2+num3)*num);
  480.   DisplayScore();
  481.   return(8);
  482.   }
  483. }
  484.  
  485. DisplayScore()
  486. {
  487. char bufferIII[30];
  488. SetAPen(rp,29);
  489. sprintf(bufferIII,"%5d",score);
  490. Move(rp,166,51);
  491. Text(rp,bufferIII,strlen(bufferIII));
  492. return();
  493. }
  494.  
  495. InitScreen()
  496. {
  497.  
  498. register int y;
  499.  
  500. SetAPen(rp,1);
  501.  
  502. for(y=191;y>=65;y-=9)
  503.  {
  504.  Move(rp,8,y); Text(rp,"*",1);
  505.  Move(rp,304,y); Text(rp,"*",1);
  506.  }
  507.  
  508. for(y=8;y<304;y+=8)
  509.  {
  510.  Move(rp,y,65); Text(rp,"*",1);
  511.  }
  512. DisplayScore();
  513. return();
  514. }
  515.  
  516.  
  517. InitLoc()
  518. {
  519. register int a,b;
  520. for(a=0;a<50;a++)
  521.  for(b=0;b<50;b++)
  522.   loc[a][b]=0;
  523. return();
  524. }
  525.  
  526.  
  527. /*************************************************************************/
  528. /*************************************************************************/
  529. /*************************************************************************/
  530. /***************************                      ************************/
  531. /***************************    Functions....     ************************/
  532. /***************************                      ************************/
  533. /*************************************************************************/
  534. /*************************************************************************/
  535. /*************************************************************************/
  536.  
  537. int idcmpch()
  538. {
  539.         if(win->UserPort->mp_SigBit)
  540.         if(message=(struct IntuiMessage *)GetMsg(win->UserPort))
  541.         {
  542.         class=message->Class;
  543.         code=message->Code;
  544.         ReplyMsg((struct IntuiMessage *)message);
  545.            switch(class)
  546.               {
  547.               case MOUSEBUTTONS:
  548.                    switch(code)
  549.                    {
  550.                    case SELECTDOWN:
  551.                    return(1);
  552.                    case MENUDOWN:
  553.                    return(2);
  554.                    }
  555.               case VANILLAKEY:
  556.               if(code<=122&&code>=97) { code-=32;
  557.               return(code); }
  558.               if(code==32) return(-2); else return(code);
  559.               }
  560.         }
  561. return(0);  /* Nothing interesting! */
  562. }
  563.  
  564.  
  565.  
  566. Open_Stuff()
  567. {
  568.  
  569. void *OpenLibrary();
  570. struct Window *OpenWindow();
  571. struct Screen *OpenScreen();
  572.  
  573. if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  574.    die("   ERROR IN STARTING UP");
  575.  
  576. if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0)))
  577.    die("   ERROR IN STARTING UP");
  578.  
  579.  
  580. if((scr=OpenScreen(&ns))==NULL)
  581.    die("   ERROR IN STARTING UP");
  582.  
  583. nw.Screen=scr;
  584.  
  585. if(!(win=(struct Window *)OpenWindow(&nw)))
  586.    die("   ERROR IN STARTING UP");
  587.  
  588. rp=win->RPort;
  589. vp=&scr->ViewPort;
  590.  
  591. SetRGB4(vp,0,0,0,0);
  592.  
  593. return(TRUE);
  594.  
  595. }
  596.  
  597.  
  598. die(s)
  599. char *s[];
  600. {
  601. char Alert[300];
  602. register int loop;
  603.  
  604. if(strlen(s)!=0) /* Display alert if s isn't NULL */
  605.    {
  606.    memset((void *)Alert,0,sizeof(Alert));
  607.    strcat(Alert,s);
  608.    strcat(Alert,"       Press either mouse button to continue.");
  609.    loop=strlen(s);
  610.    Alert[0]=0; Alert[1]=32; Alert[2]=16;
  611.    Alert[loop+2]='\0'; Alert[loop+3]=TRUE;
  612.    Alert[loop+4]=0; Alert[loop+5]=32; Alert[loop+6]=32;
  613.    Alert[loop+45]='\0'; Alert[loop+46]=FALSE;
  614.    DisplayAlert(RECOVERY_ALERT,Alert,48);
  615.    }
  616.  
  617. StopSound(LEFT0);
  618.  
  619. if(win) CloseWindow(win);
  620. if(scr) CloseScreen(scr);
  621. if(GfxBase) CloseLibrary(GfxBase);
  622. if(IntuitionBase) CloseLibrary(IntuitionBase);
  623. exit();
  624. return(TRUE);
  625. }
  626.  
  627.  
  628.